// @ts-check
"use strict";

const { pwd2 } = require("./alias-kit.js");

const isDevelopment = process.env.NODE_ENV !== "production";
const isNotDevelopment = !isDevelopment;

const devServerConfig = {
  devtool: isDevelopment ? "cheap-module-source-map" : "source-map",
  mode: process.env.NODE_ENV,
  target: /** @satisfies {unknown[]} */ (
    /** @type {const}*/ (["web", "es2022"])
  ),
  context: process.cwd(),
  // devServer
  devServer: {
    hot: true,
    liveReload: false,
    historyApiFallback: true,
    // watchFiles: {
    //   paths: ["src/**/*", "lit-comp/**/*", "./index.html"],
    //   options: {},
    // },
    port: process.env.PORT,
    proxy: [
      {
        context: "/api/token/*",
        pathRewrite: { "^/api": "" },
        target: "http://localhost:5742",
        // fin call: http://localhost:5742/token/*
      },
      {
        context: "/api",
        target: "http://localhost:5742",
        pathRewrite: { "^/api": "" },
        // fin call: http://localhost:5742
      },
    ],
    // setupMiddlewares
    devMiddleware: {
      // index: "./dist/index.html",
      // mimeTypes: { png: "image/png" },
      // publicPath: 'public',
      // serverSideRender: true,
      // writeToDisk: (filePath) => {
      //   return /^(?!.*(hot)).*/.test(filePath);
      // },
      headers: {
        "Cache-Control": "public, max-age=31536000000'",
      },
    },
  },
  // resolve
  resolve: {
    // modules: ["node_modules"],
    extensions: [".ts", ".mts", ".cts", "..."],
    alias: {
      "@": pwd2("./src"),
      "#lit-comp": pwd2("./lit-comp"),
      "#ngx-app": pwd2("./app/ngx-app"),
      "@mf": pwd2("./@mf-types"),
    },
    // extensions: [".mjs", ".js", ".cjs", ".ts"],
    // Eq: "./tsconfig.app.json"
    conditionNames: /** @type {string[]} */ (
      [isDevelopment && "development", "..."].filter(Boolean)
    ),
  },
  // externals
  externals: {
    angular: "angular",
    jquery: "jquery",
    // echarts: fg.sync("./node_modules/echarts/*.js"),
  },
};

const BAR = green`---------------------`;

/**
 * @param {TemplateStringsArray} s
 * @param {...PropertyKey} args
 * @returns {string}
 */
function red(s, ...args) {
  return `\u001B[41m${s[0]}\u001B[0m`;
}
/**
 * @param {TemplateStringsArray} s
 * @param {...PropertyKey} args
 * @returns {string}
 */
function green(s, ...args) {
  return `\u001B[42m${s[0]}\u001B[0m`;
}

module.exports = {
  BAR,
  devServerConfig,
  green,
  isNotDevelopment,
  red,
};
